<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Admin\Controller;

use Admin\Api\DatatreeApi;
use Admin\Api\UserPictureApi;
use Shop\Api\BannersApi;

class WxbannersController extends  AdminController{
    public function index(){
        $map['position'] = 1;//表示首页顶部
        $page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));
        $order = 'sort asc';
        $bannerres = apiCall(BannersApi::QUERY, array($map, $page, $order));
        if($bannerres['status']){
            $this->assign('list',$bannerres['info']['list']);
            $this->assign('show',$bannerres['info']['show']);
        }else{
            $this->error('不好意思 调出幻灯片失败');
        }
        $this->display();

    }

    /*
     * 增加幻灯片
     * */
    public function add(){
        if(IS_POST){
            $title = I('post.title','');
//			$url =
            $notes = I('post.notes','');
            $position = I('post.position',1);
            $sort = I('post.sort',0);
            if(empty($position)){
                $this->error("配置错误！");
            }
            $url_type = $this->_post('url_type',0);
            $imgid['id'] = I('img');//这里传进来的只是user_picture表中的图片id
            $imgres = apiCall(UserPictureApi::GET_INFO,array($imgid));

            if($imgres['status']){

                $entity = array(
                    'uid'=>UID,
                    'position'=>$position,
                    'storeid'=>-1,
                    'title'=>$title,
                    'notes'=>$notes,
                    'img'=>$imgres['info']['path'],
                    'url'=>I('url',''),
                    'url_type'=>$url_type,
                    'starttime'=>0,
                    'endtime'=>0,
                    'noticetime'=>0,
                    'sort'=>$sort,
                );

                $result = apiCall(BannersApi::ADD, array($entity));

                if(!$result['status']){
                    $this->error($result['info']);
                }
                $this->success("保存成功！",U('Admin/Wxbanners/index'));
            }

        }else{
            $this->display();
        }
    }

    /*
     * 编辑幻灯片
     * */
    public function edit(){
        $id = I('id',0);
        if(IS_POST){
            $title = I('post.title','');
            $notes = I('post.notes','');
            $position = I('post.position',1);
            $sort = I('post.sort',0);
            if(empty($position)){
                $this->error("配置错误！");
            }
            $url_type = $this->_post('url_type',0);
            //因为刚开始对图片API接口没有理解 及运用导致下面这段代码的产生

            $imgid['id'] = I('img');//这里传进来的只是user_picture表中的图片id
            if(is_numeric($imgid['id'])){
                $imgres = apiCall(UserPictureApi::GET_INFO,$imgid);
                if($imgres['status']) {
                    $entity['img'] = $imgres['info']['path'];
                }
            }else{
                $entity['img'] = $imgid['id'];
            }
            $entity['position'] = $position;
            $entity['title'] = $title;
            $entity['notes'] = $notes;
            $entity['url'] = I('url', '');
            $entity['url_type'] = $url_type;
            $entity['sort'] = $sort;
            $result = apiCall(BannersApi::SAVE_BY_ID, array($id, $entity));

            if(!$result['status']){
                $this->error($result['info']);
            }
            $this->success("保存成功！",U('Admin/Wxbanners/index'));
        }else{
            $result = apiCall(BannersApi::GET_INFO, array(array('id'=>$id)));
            if(!$result['status']){
                $this->error($result['info']);
            }
            $this->assign("vo",$result['info']);
            $this->display();
        }
    }


    /*
     * 幻灯片删除
     * */
    public function delete(){
        $id = I('get.id',0);
        $result = apiCall(BannersApi::DELETE,array(array('id'=>$id)));

        if(!$result['status']){
            $this->error($result['info']);
        }

        $this->success("删除成功！",U('Admin/Wxbanners/index'));

    }
}